﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class E700_AkismetTest {
		private static IWebDriver driver;
		private String forumName;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void Spammer() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.DisableMultiPost();
			driver.DisableNewUserTrial();
			driver.DisableAkismet();
			driver.LogOff();

			driver.CreateUser(Constants.Users.Spammer.Username, Constants.Users.Spammer.Password, Constants.Users.Spammer.Email);
			driver.LogOff();

			String title1 = "Akismet should leave this alone";
			driver.LogOn(Constants.Users.Spammer.Email, Constants.Users.Spammer.Password);
			driver.CreateTopic(forumName, title1, "bla bla bla bla");

			driver.LogOff();

			driver.GoToForum(forumName);
			Assert.AreEqual(true, driver.PageSource.Contains(title1), "The topic should been visible, but isn't!");

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.EnableAkismet("e891b716cf63");
			driver.LogOff();

			String title2 = "Akismet should block this topic!";
			driver.LogOn(Constants.Users.Spammer.Email, Constants.Users.Spammer.Password);
			driver.CreateTopic(forumName, title2, "AKISMET bla bla bla bla");

			driver.LogOff();

			driver.GoToForum(forumName);
			Assert.AreEqual(false, driver.PageSource.Contains(title2), "The topic shouldn't be visible, but is!");
		}
	}
}